package com.ssm.demo.util.global;


import java.util.ArrayList;
import java.util.List;

import com.ssm.demo.util.common.ApiRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 描述：     处理统一异常的handler
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object handleException(Exception e) {
        log.error("Default Exception: ", e);
        return ApiRestResponse.error(GlobalExceptionEnum.SYSTEM_ERROR.getCode(),e.toString());
    }

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Object handleBindException(BindException e) {
        log.error("BindException : ", e);
        BindingResult bindingResult = e.getBindingResult();
        return handleBindingResult(bindingResult);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Object handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        log.error("MissingServletRequestParameterException : ", e);
        return ApiRestResponse.error(GlobalExceptionEnum.NEED_PARAMS);
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public ApiRestResponse<?> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("IllegalArgumentException : ", e);
        return ApiRestResponse.error(GlobalExceptionEnum.NEED_PARAMS);
    }

    @ExceptionHandler(GlobalException.class)
    @ResponseBody
    public Object handleJAIException(GlobalException e) {
        log.error("GlobalException: ", e);
        return ApiRestResponse.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ApiRestResponse<String> handleMethodArgumentNotValidException(
            MethodArgumentNotValidException e) {
        log.error("MethodArgumentNotValidException: ", e);
        return handleBindingResult(e.getBindingResult());
    }

    private ApiRestResponse<String> handleBindingResult(BindingResult result) {
        //把异常处理为对外暴露的提示
        List<String> list = new ArrayList<>();
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError objectError : allErrors) {
                String message = objectError.getDefaultMessage();
                list.add(message);
            }
        }
        if (list.size() == 0) {
            log.error("GlobalExceptionEnum : ", new GlobalException(GlobalExceptionEnum.REQUEST_PARAM_ERROR));
            return ApiRestResponse.error(GlobalExceptionEnum.REQUEST_PARAM_ERROR);
        }
        log.error("GlobalExceptionEnum : ", new GlobalException(GlobalExceptionEnum.REQUEST_PARAM_ERROR));
        return ApiRestResponse
                .error(GlobalExceptionEnum.REQUEST_PARAM_ERROR.getCode(), list.get(0));
    }
}
